<?php
namespace globalunit\utils;


use \Swoole\Coroutine as co;
use QYS\QYS;
use QYS\Util\Dir;
use QYS\Core\Config;
use QYS\Db\Redis;

/**
 * 公用工具类
 */

Class Tools{

    //获取id号（唯一标示）
    public static function getGid($redis, $key, $keys){
        $GAMENAME = $GLOBALS['GAMENAME'];
        $key = "$GAMENAME:$key:$keys";
        $cnt = $redis->incrby($key,1);
        return (int)$cnt;
    }

    /**
     * 日志写入log文件
     * @param string  $filename 文件名称
     * @param $params
     */
    public static function writeLog($filename, $params = null)
    {
        $separator = "\t";
        if (!defined('DS')) {
            define('DS', DIRECTORY_SEPARATOR);
        }
        $t = \date("Ymd");
        $logPath = Config::getField('project', 'log_path', '');
        if (empty($logPath)) {
            $dir = QYS::getProjPath() . DS . 'log' . DS . $t;
        } else {
            $dir = $logPath . DS . $t;
        }
        $param = ['s' => $params];
        Dir::make($dir);
        $str = \date('Y-m-d H:i:s', Config::get('now_time', time())) . $separator  . $separator .  $separator . \implode($separator, array_map('QYS\Log\Log::myJson', $param));
        $logFile = $dir . \DS . $filename . '.log';
        co::create(function () use ($logFile,$str)
        {
            $r =  co::writeFile($logFile, $str . "\n",FILE_APPEND);
        });
    }

}
